/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef NODE_DB_SQL_OPERATION_H_
#define NODE_DB_SQL_OPERATION_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include "node/util/lockedqueue.h"
#include "node/db/callback.h"
#include "node/system/mutex.h"

namespace node {
    namespace db {
        class Connection;
        class SqlStmtParameters;

        class SqlOperation
        {
        public:
            virtual ~SqlOperation(){}
            virtual bool execute(::node::db::Connection *) = 0;
        };

        class SqlPreparedRequest : public SqlOperation
        {
        public:
            SqlPreparedRequest(int nIndex, SqlStmtParameters* arg);
            virtual ~SqlPreparedRequest();

            bool execute(::node::db::Connection* conn) override;

        private:
            const int index_;
            SqlStmtParameters* param_;
        };

        class SqlResultQueue : public ::node::util::LockedQueue<::node::db::IQueryCallback* , ::node::system::Mutex>
        {
        public:
            SqlResultQueue() {}
            void update();
        };

        class SqlQuery : public SqlOperation
        {
        public:
            SqlQuery(const char* sql, node::db::IQueryCallback* callback, SqlResultQueue* queue)
                : sql_(sql), callback_(callback), queue_(queue) {}
            ~SqlQuery() {  }
            bool execute(Connection* conn) override;

        private:
            const ::std::string sql_;
            node::db::IQueryCallback* callback_;
            SqlResultQueue* queue_;
        };

    }
}

#endif // NODE_DB_SQL_OPERATION_H_
